package com.contract.spirit.manage

import android.content.Context
import com.contract.spirit.func.dialog.CommonRuleTipsDialog
import com.contract.spirit.bean.response.MainPageResponse.ExchangePollInfo
import com.contract.commonlib.util.CommonUtils
import com.contract.spirit.bean.entity.DialogBean
import com.contract.spirit.func.dialog.base.BasePopDataBindImpl.OnDialogCallBack
import com.contract.spirit.ui.exchange.bind.BindExchangeActivity
import com.contract.spirit.ui.exchange.ExchangeRegisterUrlActivity
import java.lang.ref.SoftReference

/**
 * @author zhengjun
 * @desc
 * @create at 2021/6/22 11:12
 */
object BindExchangeDialogManager {
    var dialogWeakReference: SoftReference<CommonRuleTipsDialog>? = null

    /**
     * 未绑定交易所 提示绑定
     */
    fun showBindExchangeDialog(context: Context?, item: ExchangePollInfo) {
        if (CommonUtils.isPopShow(softReferenceDialog)) {
            return
        }
        val dialogBean = DialogBean()
        dialogBean.isShowClose = true
        dialogBean.leftBtnText = String.format("点击链接注册%s", item.getExhcangeName())
        dialogBean.rightBtnText = "已下载，接入实盘"
        dialogBean.subTitle = "接入实盘需要先通过我们平台下载注册交易所,才可以获得合约赔付。"
        dialogBean.title = "接入实盘提示"
        val bindExchangeTipDialog =
            CommonRuleTipsDialog.show(context!!, dialogBean, object : OnDialogCallBack {
                override fun onRightBtnClick() {
                    closeDialog()
                    BindExchangeActivity.launch(context, item.exchangeType);
                }
                override fun onLeftBtnClick() {
                    closeDialog()
                     ExchangeRegisterUrlActivity.launch(context,item.getRegisterExchangeUrl(),item.getExchangeInviteCode());
                }
                override fun onClose() {
                    clearDialog()
                }
            })
        dialogWeakReference = SoftReference(bindExchangeTipDialog)
    }

    private fun clearDialog() {
        if (dialogWeakReference != null) {
            dialogWeakReference!!.clear()
        }
    }

    private fun closeDialog() {
        if (softReferenceDialog != null) {
            softReferenceDialog!!.dismiss()
        }
        clearDialog()
    }

    private val softReferenceDialog: CommonRuleTipsDialog?
        get() = if (dialogWeakReference != null) {
            dialogWeakReference!!.get()
        } else null
}